package erfgame.core.graphics.renderer.chain;

import java.awt.Dimension;
import java.awt.Point;


import erfgame.core.Rotation;
import erfgame.core.entity.Entity;
import erfgame.core.graphics.renderer.AbstractEntityRenderer;
import erfgame.core.graphics.renderer.EntityRenderer;
import erfgame.core.graphics.renderer.RendererUpdateListener;
import erfgame.core.graphics.renderer.RendererUpdateType;



public abstract class AbstractChainedRenderer
		extends AbstractEntityRenderer 
		implements ChainedRenderer, RendererUpdateListener {
	
	protected EntityRenderer child;
	private int priority;

	public EntityRenderer getChild() {
		return this.child;
	}
	
	@Override
	public Entity getEntity() {
		EntityRenderer child = getChild();
		Entity result;
		if( child != null ) {
			result = child.getEntity();
		} else {
			result = super.getEntity();
		}
		return result;
	}

	public int getPriority() {
		return this.priority;
	}
	
	public void setPriority( int priority ) {
		this.priority = priority;
	}
	
	@Override
	public Dimension getDimension() {
		return getChild().getDimension();
	}
	
	@Override
	public Point getGrabPoint() {
		return getChild().getGrabPoint();
	}

	public void setChild(EntityRenderer child, boolean started) {
		if( this.child != null ) {
			this.child.setUpdateListener(null);
		}
		this.child = child;
		if( child != null && started ) {
			child.setUpdateListener(this);
		}
	}
	
	@Override
	public boolean contains(int lx, int ly) {
		boolean b;
		if( this.child != null ) {
			b = child.contains(lx, ly);
		} else {
			b = super.contains(lx, ly);
		}
		return b;
	}

	public void rendererUpdated(RendererUpdateType type, Object state) {
		fireUpdate(type, state);
	}

	@Override
	public void start() {
		EntityRenderer child = this.child;
		if( child != null ) {
			child.setUpdateListener(this);
		}
		super.start();
	}

	@Override
	public void stop() {
		EntityRenderer child = this.child;
		if( child != null ) {
			child.setUpdateListener(null);
		}
		super.stop();
	}

	@Override
	public int getDepth(Rotation rotation) {
		if( child != null ) {
			return this.child.getDepth(rotation);
		} else{
			return super.getDepth(rotation);
		}
	}
	
	

}
